<?php
/*
* $Author ：PHPYUN开发团队
*
* 官网: http://www.phpyun.com
*
* 版权所有 2009-2018 宿迁鑫潮信息技术有限公司，并保留所有权利。
*
* 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
 */
class wxapp_controller extends common{ 


	function __construct($tpl,$db,$def="",$model="index",$m="") {
		$this->common($tpl,$db,$def,$model,$m);
		$this->wxapp();
	}

	
	function wxapp(){
	    $id = 'wxf2632dab1e187851';
	    $key = '123456';
	    include APP_PATH."/api/wxapp/PHP/openssl.php";
	    $openssl = new OpensslCrypt($id,$key);
	    $Safekey = substr($openssl->encrypt(), -40,32);
            //echo $Safekey;
        if($_GET['safekey'] && $_GET['safekey']==$Safekey){
            return true;
	    }else{
	        echo json_encode(array('error'=>'1001'.$_GET['safekey']));
	        exit();
	    }
	}
	function preghtml($str){
	    $return = preg_replace("/<span[^>]*?>(.*?)<\/span>/is","$1",$str);
	    while (strpos($return,'</span>')!==false){
	        $return = preg_replace("/<span[^>]*?>(.*?)<\/span>/is","$1",$return);
	    }
	    $return = preg_replace("/<div[^>]*?>(.*?)<\/div>/is","$1",$return);
	    while (strpos($return,'</div>')!==false){
	        $return = preg_replace("/<div[^>]*?>(.*?)<\/div>/is","$1",$return);
	    }
	    $return = preg_replace("/<font[^>]*?>(.*?)<\/font>/is","$1",$return);
	    while (strpos($return,'</font>')!==false){
	        $return = preg_replace("/<font[^>]*?>(.*?)<\/font>/is","$1",$return);
	    }
	    return $return;
	}
	function getOpenid($code){
	    include(dirname(dirname(dirname(__FILE__)))."/data/api/wxpay/wxpay_data.php");
	    $appid=$wxpaydata['sy_xcxappid']; 
	    $secert=$wxpaydata['sy_xcxsecret']; 
	    
	    $token_url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $secert . "&js_code=".$code."&grant_type=authorization_code";
	    if(function_exists('curl_init')) {
	        $ch = curl_init();
	        curl_setopt($ch, CURLOPT_URL,$token_url);
	        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	        $response=curl_exec ($ch);
	        curl_close ($ch);
	        
	        $user = json_decode($response,true);
	        return $user;
	    }else{
	        return 1;
	    }
	}
	function errjson($errmsg,$error=1){
	    $arr['error'] = $error;
	    $arr['errmsg']  = $errmsg;
	    echo json_encode($arr);die;
	}
	function yzsession3rd($uid,$session3rd){
	    $user=$this->obj->DB_select_once("member","`uid`='".$uid."'",'`session3rd`');
	    if (!$user['session3rd'] || $user['session3rd']!=$session3rd){
	        $data['errmsg']='请先登录';
	        $data['error']=2;
	        echo json_encode($data);die;
	    }
	}
	function member_log($content,$uid,$usertype,$opera='',$type=''){
	    if($uid){
	        $value="`uid`='".$uid."',";
	        $value.="`usertype`='".$usertype."',";
	        $value.="`content`='".$content."',";
	        $value.="`opera`='".$opera."',";
	        $value.="`type`='".$type."',";
	        $value.="`ip`='".fun_ip_get()."',";
	        $value.="`ctime`='".time()."'";
	        $this->obj->DB_insert_once("member_log",$value);
	        $this->login_log($uid);
	    }
	}
	function login_log($uid){
	    if (!empty($uid)){
	        $userInfoM = $this->MODEL('userinfo');
	        $member = $userInfoM->GetMemberOne(array('uid'=>intval($_POST['uid'])),array('field'=>'`uid`,`usertype`'));
	        if (!empty($member)){
	            //今日没有登录的，记录登录日志
	            $loginnum = $this->obj->DB_select_num('login_log','`uid`='.$member['uid'].' AND `ctime`>'.strtotime('today').'');
	            if ($loginnum < 1){
	                $time = time();
	                $ip = fun_ip_get();
	                $logindata = array(
	                    'uid'      => $member['uid'],
	                    'usertype' => $member['usertype'],
	                    'content'  => '小程序登录成功',
	                    'ip'       => $ip,
	                    'ctime'    => $time
	                );
	                $this->obj->insert_into('login_log',$logindata);
	                $userInfoM->UpdateMember(array('login_ip'=>$ip,'login_date'=>$time),array('uid'=>$member['uid']));
	                $this->MODEL('integral')->get_integral_action($member['uid'],"integral_login","会员登录");
	            }
	        }
	    }
	}
}
?>